import XlsxData from "../xlsx/XlsxData";
import GlobalEvent from "../event/GlobalEvent";
export default class LangManager {

    private langName: string;
    private data: XlsxData;
    private static ins: LangManager;

    static instance(): LangManager {
        if (!this.ins) {
            this.ins = new LangManager();
        }
        return this.ins;
    }

    constructor() {

    }


    getLang() {
        return this.langName;
    }

    clear(): void {
        this.data = null;
    }

    setLang(langName: string, data: XlsxData) {
        if (this.langName == langName) {
            return;
        }
        this.data = data;
        // cc.log(' langName ',langName)
        this.langName = langName;
        GlobalEvent.instance().emit(GlobalEvent.CHANGE_LANG)
    }

    getLocalString(langID: string, opt?) {

        if (!this.data) {
            console.warn("LangManager is not init localString langID  ", langID, this.data);
            return langID;
        }
        let str: string = this.data.getValue(langID);
        if (str) {
            if (str.indexOf('\\') >= 0) {
                str = str.replace(/\\n/g, '\n');
            }
            if (opt) {
                for (const key in opt) {
                    let value = opt[key];
                    str = str.replace("%{" + key + "}", value);

                }
            }
        } else {
            str = langID;
        }
        return str;
    }


}
